package com.topfinance.cnaps2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.topfinance.cnaps2.dto.ConfRouteRuleDto;
import com.topfinance.cnaps2.ebo.ConfInPortEbo;
import com.topfinance.cnaps2.ebo.ConfOutPortEbo;
import com.topfinance.cnaps2.ebo.ConfRouteRuleEbo;
import com.topfinance.cnaps2.service.IRouteRuleManager;
import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class RouteRuleManagerImpl extends HibernateEntityDao<ConfRouteRuleEbo> implements IRouteRuleManager {

	private static final Log log = LogFactory.getLog(RouteRuleManagerImpl.class);

	public void addRoute(ConfRouteRuleEbo o) throws AppException{
    	save(o);
    }
	
	public void addRoute(ConfRouteRuleDto rt) throws AppException{
		ConfInPortEbo pi = (ConfInPortEbo)get(ConfInPortEbo.class,rt.getIPID());
		ConfOutPortEbo po = (ConfOutPortEbo)get(ConfOutPortEbo.class,rt.getOPID());
		ConfRouteRuleEbo rr = new ConfRouteRuleEbo();
		BeanUtils.copyProperties(rt, rr);
		rr.setOutportEbo(po);
		rr.setInportEbo(pi);
		saveOrUpdate(rr);		
	}
	
	public void editRoute(ConfRouteRuleDto rt) throws AppException{
		ConfInPortEbo pi = (ConfInPortEbo)get(ConfInPortEbo.class,rt.getIPID());
		ConfOutPortEbo po = (ConfOutPortEbo)get(ConfOutPortEbo.class,rt.getOPID());
		ConfRouteRuleEbo rr = new ConfRouteRuleEbo();
		BeanUtils.copyProperties(rt, rr);
		rr.setOutportEbo(po);
		rr.setInportEbo(pi);
		update(rr);
    }
	
	//获得传输协议	
	public ConfRouteRuleEbo getRoute(String uid) throws AppException{
		return (ConfRouteRuleEbo)get(uid);
	}

	public void deleteRoute(ConfRouteRuleEbo o) throws AppException{
		removeById(o.getClass(),o.getUid());
    }
	public void deleteRoute(Integer id) throws AppException{
		removeById(ConfRouteRuleEbo.class,id);
	}

	//获得传输协议列表
	public List getAllRouteList() throws AppException{
		String hql = " from ConfRouteRuleEbo t " ;
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
}
